<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    .backtop {
      position: fixed;
      right: 20px;
      bottom: 20px;
      width: 45px;
      line-height: 45px;
      text-align: center;
      background-color: rgba(0, 0, 0, 0.6);
      border-radius: 50%;
      color: #fff;
      font-size: 30px;
      -webkit-top-highlight-color: transparent;
      transform: translate3d(x, y, 0);
    }
  </style>
</head>

<body>
  <a href="#" id="backtop" class="backtop">&uarr;</a>
  <script>
    function drag(el, options) {
      options.x = typeof options.x !== 'undefind' ? options.x : true;
      options.y = typeof options.y !== 'undefind' ? options.y : true;
      if (!options.x && !options.y) return;
      let curPoint={x:0,y:0}
      let startPoint = {}
      el.addEventListener('touchstart', handleStart, false)
      el.addEventListener('touchmove', handleMove, false)
      el.addEventListener('touchend', handleEnd, false)
      function handleStart(e) {
        let touch = e.changedTouches[0]
        startPoint.x = touch.pageX;
        startPoint.y = touch.pageY;
      }
      function handleMove(e) {
        let touch = e.changedTouches[0];
        let diffPoint={};
        let movePoint={x:0,y:0};
        diffPoint.x=touch.pageX-startPoint.x;
        diffPoint.y=touch.pageY-startPoint.y;
        if(options.x){
          movePoint.x=diffPoint.x+curPoint.x
        }
        if(options.y){
          movePoint.y=diffPoint.y+curPoint.y
        }
        move(el,movePoint.x,movePoint.y)
      }
      function handleEnd(e) {
        let touch = e.changedTouches[0];
        curPoint.x+=touch.pageX-startPoint.x
        curPoint.y+=touch.pageY-startPoint.y
      }
      function move(el, x, y) {
        x = x || 0;
        y = y || 0;
        el.style.transform = `translate3d(${x}px,${y}px,0)`;
      }
    }
  </script>
  <script>
    let backtop = document.getElementById('backtop');
    console.log(backtop);
    drag(backtop, { x: true, y: true })

    // let curPoint={x:0,y:0};
    // backtop.addEventListener('click', function () {
    //   move(this, -10, -10);
    // }, false)


  </script>
</body>

</html>